package org.ftc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.ftc.domain.CodeCause;
import org.ftc.form.CodeCauseForm;
import org.ftc.service.CodeCauseService;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller 
public class CodeCauseController {
	@Autowired
	UsersService usersService;
	@Autowired
	CodeCauseService codeCauseService;
	@Autowired
	FtcLogService logService;
	
	@RequestMapping(value = "/codeCause.htm")
	public String codeCause(ModelMap model) {
		CodeCauseForm searchForm = new CodeCauseForm();
		model.addAttribute("searchForm", searchForm);//
		return "codeCause/list";
	}

	@RequestMapping(value = "/codeCause/list.json", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> find(CodeCauseForm form, BindingResult binding, ModelMap model) {
		logService.before();
		Map<String, Object> json = new HashMap<String, Object>();
		List<CodeCause> data = codeCauseService.findPage(form);
		json.put("rows", data);
		logService.save(Log.QUERY, this.getClass().toString());
		return json;
	}
	
	@RequestMapping(value = "/codeCause/update.json", method = RequestMethod.POST)
	public @ResponseBody Object save(CodeCause entity,BindingResult binding, ModelMap model) {
		logService.before();
		if(entity==null || StringUtils.isBlank(entity.getName())){
			return true;
		}
		if(StringUtils.isBlank(entity.getCode())){
			codeCauseService.save(entity);
			logService.save(Log.INSERT, entity.getCode(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		}else{
			CodeCause codeCause = codeCauseService.get(entity.getCode());
			codeCause.setCode(entity.getCode());
			codeCause.setName(entity.getName());
			codeCauseService.update(codeCause);
			logService.save(Log.UPDATE, entity.getCode(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		}
		return entity;
	}
	
	@RequestMapping(value = "/codeCause/delete/{codeCauseSeq}.json")
	public @ResponseBody String delete(@PathVariable String codeCauseSeq) {
		logService.before();
		Boolean s= false;
		if(StringUtils.isBlank(codeCauseSeq)){
			return s.toString();
		}
		s = codeCauseService.delete(codeCauseSeq);
		logService.save(Log.DELETE, codeCauseSeq, this.getClass().toString());
		return s.toString();  
	}
	
	@RequestMapping(value = "/codeCause/find.json",method = RequestMethod.GET)
	public @ResponseBody List<CodeCause> find() {
		return codeCauseService.findAll();
	}

}
